package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.ChannelInfiltrationRate;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.RainfallInfiltrationRate;
import com.sevenyear.excelanalyze.service.ChannelInfiltrationRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import lombok.Data;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/29 20:16
 */
public class ChannelInfiltrationRateListener extends AnalysisEventListener<ChannelInfiltrationRate> {


    private PartitionBaseInfoService partitionBaseInfoService;

    private ChannelInfiltrationRateService channelInfiltrationRateService;

    public ChannelInfiltrationRateListener() {
    }

    public ChannelInfiltrationRateListener(PartitionBaseInfoService partitionBaseInfoService, ChannelInfiltrationRateService channelInfiltrationRateService) {
        this.partitionBaseInfoService = partitionBaseInfoService;
        this.channelInfiltrationRateService = channelInfiltrationRateService;
    }


    @Override
    public void invoke(ChannelInfiltrationRate channelInfiltrationRate, AnalysisContext analysisContext) {
        //计算平均值
        System.out.println(channelInfiltrationRate);
        float average = CalculateUtils.average(channelInfiltrationRate.getChannelInfiltrationRateMin(), channelInfiltrationRate.getChannelInfiltrationRateMax());
        channelInfiltrationRate.setChannelInfiltrationRateAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",channelInfiltrationRate.getLandform()).eq("clay_thickness",channelInfiltrationRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        channelInfiltrationRate.setPartitionId(partitionBaseInfo.getId());
        channelInfiltrationRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        channelInfiltrationRate.setAreaCode(channelInfiltrationRate.getArea()+channelInfiltrationRate.getPartitionCode());
        UpdateWrapper<ChannelInfiltrationRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",channelInfiltrationRate.getAreaCode());
        channelInfiltrationRateService.saveOrUpdate(channelInfiltrationRate,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
